package com.xinggq.user.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinggq.common.CommonResult;
import com.xinggq.common.page.PageCondition;
import com.xinggq.user.service.dto.role.RoleCreateRequest;
import com.xinggq.user.service.dto.role.RolePageRequest;
import com.xinggq.user.service.dto.role.RoleUpdateRequest;
import com.xinggq.user.service.dto.role.RoleVO;
import com.xinggq.user.service.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 角色管理控制器
 *
 * @author system
 * @since 2025-01-10
 */
@RestController
@RequestMapping("/api/role")
@Tag(name = "角色管理", description = "角色管理相关接口")
@Slf4j
@AllArgsConstructor
@Validated
public class RoleController {

    private final RoleService roleService;

    /**
     * 创建角色
     */
    @PostMapping("/create")
    @Operation(summary = "创建角色", description = "创建新的角色信息")
    public CommonResult<RoleVO> createRole(@Valid @RequestBody RoleCreateRequest request) {
        RoleVO result = roleService.createRole(request);
        return CommonResult.ok(result);
    }

    /**
     * 更新角色
     */
    @PostMapping("/update")
    @Operation(summary = "更新角色", description = "根据角色ID更新角色信息")
    public CommonResult<RoleVO> updateRole(
            @Valid @RequestBody RoleUpdateRequest request) {
        RoleVO result = roleService.updateRole(request);
        return CommonResult.ok(result);
    }

    /**
     * 获取角色详情
     */
    @GetMapping("/get-detail")
    @Operation(summary = "获取角色详情", description = "根据角色ID获取角色详细信息")
    public CommonResult<RoleVO> getRoleDetail(
            @Parameter(description = "角色ID", required = true)
            @RequestParam(value = "roleId")
            Long roleId) {
        RoleVO result = roleService.getRoleDetailById(roleId);
        return CommonResult.ok(result);
    }

    /**
     * 分页查询角色
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询角色", description = "根据条件分页查询角色列表，支持角色名称、编码、描述等模糊查询")
    public CommonResult<IPage<RoleVO>> pageQuery(@Valid @RequestBody PageCondition<RolePageRequest> pageCondition) {
        log.info("分页查询角色请求: {}", pageCondition);
        IPage<RoleVO> result = roleService.pageQuery(pageCondition);
        return CommonResult.ok(result);
    }

    /**
     * 删除角色（逻辑删除）
     */
    @GetMapping("/del")
    @Operation(summary = "删除角色", description = "根据角色ID逻辑删除角色（通过status实现）")
    public CommonResult<Boolean> deleteRole(
            @Parameter(description = "角色ID", required = true)
            @RequestParam(value = "roleId")
            Long roleId) {
        log.info("删除角色请求: roleId={}", roleId);
        boolean result = roleService.deleteRole(roleId);
        return CommonResult.ok(result);
    }
}